package com.woniuxy.joylifeforum.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeforum.dto.TopicDto;
import com.woniuxy.joylifeforum.service.TopicService;
import com.woniuxy.joylifeforum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 话题，回复，点赞，粉丝等主业务
 */
@RestController
public class MyController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private UserService userService;
    @RequestMapping("/test")
    public Object test() {
        System.out.println("结果：测试" );
        return 1;
    }

    /**
     * 查询首页最新
     */
    @RequestMapping("/debate")
    public Object debate(Integer page, Integer limit,HttpServletRequest request) {
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        //System.out.println(request.getHeader("token"));
        //System.out.println(userId);
        //当前用户id
        //String userId="111";
        PageInfo<TopicDto> TopicDtoPH = topicService.findTopic(userId,page, limit);
        Map<String,Object>result=new HashMap<>();
        result.put("code",200);
        result.put("mes","OK");
        result.put("data",TopicDtoPH);
        return result;
    }
    /**
     * 查询首页我的关注
     */
    @RequestMapping("/concern")
    public Object concern(Integer page,Integer limit,HttpServletRequest request) {
        //当前用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        PageInfo<TopicDto> TopicDtoPH = topicService.findTopic(page, limit,userId);
        Map<String,Object>result=new HashMap<>();
        result.put("code",200);
        result.put("mes","OK");
        result.put("data",TopicDtoPH);
        return result;
    }
    /**
     * 查询首页我的社区话题
     */
    @RequestMapping("/myForumTopic")
    public Object myForumTopic(Integer page,Integer limit,HttpServletRequest request) {
        //当前用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Map<String,Object>result=new HashMap<>();
        try {
            PageInfo<TopicDto> TopicDtoPH = topicService.findMyForumTopic(page, limit,userId);
            result.put("code",200);
            result.put("mes","OK");
            result.put("data",TopicDtoPH);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code",400);
            result.put("mes",e.getMessage());
            result.put("data",null);
        }
        return result;
    }
    /**
     * 查询首页搜索
     */
    @RequestMapping("/search")
    public Object search(Integer page,Integer limit,String find,HttpServletRequest request) {
        //当前用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        PageInfo<TopicDto> TopicDtoPH = topicService.findTopic(userId,find,page, limit);
        Map<String,Object>result=new HashMap<>();
        result.put("code",200);
        result.put("mes","OK");
        result.put("data",TopicDtoPH);
        return result;
    }
    /*
    获取指定用户id
     */
    @RequestMapping("/findUserMes")
    public Object findUserMes(String centerUser,HttpServletRequest request) {
        //当前用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        if("null".equals(centerUser)){
            centerUser=userId;
        }
        Map<String, Object> result = userService.findUser(userId,centerUser);
        result.put("code",200);
        result.put("mes","OK");
        return result;
    }
    /*
    用户发送的  话题 回复 收藏
     */
    @RequestMapping("/userTopicMes")
    public Object userTopicMes(Integer site,String findUserId,Integer page, Integer limit,HttpServletRequest request) {
        //当前用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        if("null".equals(findUserId)){
            findUserId=userId;
        }
        if(site==1){
            //查询用户发送的话题
            PageInfo<TopicDto> data = userService.findSendTopic(userId, findUserId, page, limit);
            Map<String, Object> result = new HashMap<>();
            result.put("code",200);
            result.put("data",data);
            result.put("mes","OK");
            return result;
        }else if(site==2){
            PageInfo<Map<String, Object>> data = userService.findReplyTopic(userId,findUserId, page, limit);
            Map<String, Object> result = new HashMap<>();
            result.put("code",200);
            result.put("data",data);
            result.put("mes","OK");
            return result;
        }else if(site==3){
            PageInfo<TopicDto> data = userService.findCollectTopic(userId, findUserId, page, limit);
            Map<String, Object> result = new HashMap<>();
            result.put("code",200);
            result.put("data",data);
            result.put("mes","OK");
            return result;
        }else{
            return null;
        }
    }
    /*
    用户关注，取消关注他人
     */
    @RequestMapping("/concernsUser")
    public Object concernsUser(Integer status,String targetUserId,HttpServletRequest request) {
        //当前用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Integer data;
        if(status==1){
            //添加关注
            data = userService.addConcerns(userId, targetUserId);
        }else {
            data = userService.cancelConcerns(userId, targetUserId);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code",200);
        result.put("data",data);
        result.put("mes","OK");
        return result;
    }
    /*
   删除收藏，话题
    */
    @RequestMapping("/delContent")
    public Object delContent(Integer site,String delId,HttpServletRequest request) {
        //当前用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Integer data;
        if(site==1){
            //删除话题
            data = topicService.delTopic(userId,delId);
        }else {
            //删除收藏
            data = topicService.delConcern(userId,delId);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code",200);
        result.put("data",data);
        result.put("mes","OK");
        return result;
    }
}
